package com.example.unique

import android.os.Bundle
import android.widget.EditText
import android.widget.Spinner
import androidx.appcompat.app.AppCompatActivity
import android.content.Intent
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.Button
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import com.example.unique.database.DatabaseHelper
class RegisterActivity : AppCompatActivity() {

    private lateinit var academy: Spinner
    private lateinit var club: Spinner
    private lateinit var name: EditText
    private lateinit var role: Spinner
    private lateinit var snoName: EditText
    private lateinit var passName: EditText
    private lateinit var repassName: EditText
    private lateinit var dbHelper: DatabaseHelper
    lateinit var acedemyInfo: String
    lateinit var clubInfo: String
    lateinit var roleInfo: String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.register)
        var filledNum = 0
        val register = findViewById<Button>(R.id.register)
        snoName = findViewById(R.id.sno)
        passName = findViewById(R.id.register_password)
        repassName = findViewById(R.id.register_repassword)
        academy = findViewById(R.id.spinner)
        club = findViewById(R.id.club)
        name = findViewById(R.id.name)
        role = findViewById(R.id.spinner2)

        dbHelper = DatabaseHelper(this)

        academy.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
                acedemyInfo = parent.getItemAtPosition(position).toString()
            }

            override fun onNothingSelected(parent: AdapterView<*>) {filledNum++}
        }
        club.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
                clubInfo = parent.getItemAtPosition(position).toString()
            }

            override fun onNothingSelected(parent: AdapterView<*>) {filledNum++}
        }
        role.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
                roleInfo = parent.getItemAtPosition(position).toString()
            }

            override fun onNothingSelected(parent: AdapterView<*>) {filledNum++}
        }

        register.setOnClickListener {
            val passNameInfo = passName.text.toString()
            val repassNameInfo = repassName.text.toString()

            if (repassNameInfo != passNameInfo) {
                Toast.makeText(this, "两次密码不一致，请重新输入！", Toast.LENGTH_SHORT).show()
            } else {
                // 如果是负责人
                if (roleInfo == "社团负责人") {
                    // 检查学号是否为负责人
                    if (dbHelper.isLeader(snoName.text.toString())) {
                        Toast.makeText(this, "您是负责人，正在注册...", Toast.LENGTH_SHORT).show()
                        // 插入数据库并设置 role 为 1 (负责人)
                        dbHelper.insertUser(acedemyInfo, clubInfo, name.text.toString(), 1, snoName.text.toString(), passName.text.toString())
                        val intent = Intent()
                        intent.putExtra("sInfo", snoName.text.toString())
                        setResult(1, intent)
                        finish()
                    } else {
                        Toast.makeText(this, "您不是负责人，请重新修改身份信息", Toast.LENGTH_SHORT).show()
                    }
                } else {
                    // 如果是普通用户
                    dbHelper.insertUser(acedemyInfo, clubInfo, name.text.toString(), 2, snoName.text.toString(), passName.text.toString())
                    val intent = Intent()
                    intent.putExtra("sInfo", snoName.text.toString())
                    setResult(1, intent)
                    finish()
                }
            }
        }

    }
}